package com.example.my_bookkeeping.data.user;

import java.util.ArrayList;
import java.util.HashMap;

public class User {
    private static long MAIN_KEY = 0;
    // 定义用户信息储存方式
    private final long _id;
    private String name;
    private String remark;
    private double amount;

    // 储存记账记录，同时使用工厂模式创建BookKeep对象
    private long BOOK_KEEP_MAIN_KEY = 0;  // 使用自增主键作为记账哈希表的键，避免重复
    private final HashMap<Long, BookKeep> bookKeepBank = new HashMap<>();

    public User(String name, String remark, double amount) {
        if(MAIN_KEY == Long.MAX_VALUE)
            throw new RuntimeException("User Count Max Error");
        this._id = MAIN_KEY++;
        this.name = name;
        this.remark = remark;
        this.amount = amount;
    }

    public long getId() {
        return _id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public ArrayList<BookKeep> getAllBookKeeps() {
        return new ArrayList<>(bookKeepBank.values());
    }

    // 添加一个记账信息，同时将其添加到bookKeepBank中
    public void addBookKeep(String time, double amount, String category, String remark, boolean isIncome) {
        if(BOOK_KEEP_MAIN_KEY == Long.MAX_VALUE)
            throw new RuntimeException("Book Keep Count Max Error");
        BookKeep bookKeep = new BookKeep(BOOK_KEEP_MAIN_KEY, time, amount, category, remark, isIncome);
        BOOK_KEEP_MAIN_KEY++;

        bookKeepBank.put(bookKeep.getId(), bookKeep);
    }

    // 删除一个记账信息
    public void deleteBookKeep(BookKeep bookKeep) {
        bookKeepBank.remove(bookKeep.getId());
    }

    // 更新一个记账信息（接收字段参数）
    public void updateBookKeepFields(long id, double amount, String category, String remark) {
        BookKeep bookKeep = bookKeepBank.get(id);
        if (bookKeep != null) {
            bookKeep.setAmount(amount);
            bookKeep.setCategory(category);
            bookKeep.setRemark(remark);
        }
    }

    // 重置静态变量
    public static void reSet(){
        MAIN_KEY = 0;
    }
}
